<template>
	<view class="register-container">
		<view class="form-container">
			<view class="form-item">
				<text class="label">用户名</text>
				<input type="text" v-model="formData.username" placeholder="请输入用户名" class="input" />
			</view>
			
			<view class="form-item">
				<text class="label">密码</text>
				<input type="password" v-model="formData.password" placeholder="请输入密码" class="input" />
			</view>
			
			<view class="form-item">
				<text class="label">确认密码</text>
				<input type="password" v-model="formData.confirmPassword" placeholder="请再次输入密码" class="input" />
			</view>
			
			<view class="form-item">
				<text class="label">手机号</text>
				<input type="number" v-model="formData.phone" placeholder="请输入手机号" class="input" />
			</view>
			
			<button class="register-btn" @click="handleSubmit">注册</button>
			
			<view class="login-link">
				<text>已有账号？</text>
				<text class="link" @click="goToLogin">立即登录</text>
			</view>
		</view>
	</view>
</template>

<script>
	import { register } from '@/api/user.js'
	
	export default {
		data() {
			return {
				formData: {
					username: '',
					password: '',
					confirmPassword: '',
					phone: ''
				}
			}
		},
		methods: {
			// 检查密码强度
			checkPasswordStrength(password) {
				let strength = 0
				
				// 长度检查
				if (password.length >= 8) {
					strength++
				}
				
				// 包含数字
				if (/\d/.test(password)) {
					strength++
				}
				
				// 包含小写字母
				if (/[a-z]/.test(password)) {
					strength++
				}
				
				// 包含大写字母
				if (/[A-Z]/.test(password)) {
					strength++
				}
				
				// 包含特殊字符
				if (/[!@#$%^&*]/.test(password)) {
					strength++
				}
				
				return strength
			},
			
			// 提交表单
			async handleSubmit() {
				// 表单验证
				if (!this.formData.username) {
					uni.showToast({
						title: '请输入用户名',
						icon: 'none'
					})
					return
				}
				
				if (!this.formData.password) {
					uni.showToast({
						title: '请输入密码',
						icon: 'none'
					})
					return
				}
				
				if (!this.formData.confirmPassword) {
					uni.showToast({
						title: '请确认密码',
						icon: 'none'
					})
					return
				}
				
				if (this.formData.password !== this.formData.confirmPassword) {
					uni.showToast({
						title: '两次输入的密码不一致',
						icon: 'none'
					})
					return
				}
				
				// 检查密码强度
				const strength = this.checkPasswordStrength(this.formData.password)
				if (strength < 2) {
					uni.showToast({
						title: '密码强度不足，请包含数字和字母',
						icon: 'none'
					})
					return
				}
				
				if (!this.formData.phone) {
					uni.showToast({
						title: '请输入手机号',
						icon: 'none'
					})
					return
				}
				
				if (!/^1[3-9]\d{9}$/.test(this.formData.phone)) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none'
					})
					return
				}
				
				let loadingShown = false
				try {
					// 显示加载提示
					uni.showLoading({
						title: '注册中...'
					})
					loadingShown = true
					
					// 发送注册请求
					const res = await register({
						username: this.formData.username,
						password: this.formData.password,
						phone: this.formData.phone
					})
					
					if (res.code === 200) {
						uni.showToast({
							title: '注册成功',
							icon: 'success'
						})
						// 注册成功后跳转到登录页
						setTimeout(() => {
							uni.navigateTo({
								url: '/pages/login/login'
							})
						}, 1500)
					} else {
						uni.showToast({
							title: res.msg || '注册失败',
							icon: 'none'
						})
						console.error('注册失败:', res)
					}
				} catch (err) {
					console.error('注册请求失败:', err)
					uni.showToast({
						title: err.msg || '网络错误，请稍后重试',
						icon: 'none'
					})
				} finally {
					if (loadingShown) {
						uni.hideLoading()
					}
				}
			},
			goToLogin() {
				uni.redirectTo({
					url: '/pages/login/login'
				})
			}
		}
	}
</script>

<style>
	.register-container {
		padding: 40rpx;
		min-height: 100vh;
		background-color: #fff;
	}
	
	.form-container {
		margin-top: 40rpx;
	}
	
	.form-item {
		margin-bottom: 30rpx;
	}
	
	.label {
		display: block;
		font-size: 28rpx;
		color: #333;
		margin-bottom: 10rpx;
	}
	
	.input {
		width: 100%;
		height: 80rpx;
		border: 1rpx solid #ddd;
		border-radius: 8rpx;
		padding: 0 20rpx;
		font-size: 28rpx;
	}
	
	.register-btn {
		width: 100%;
		height: 88rpx;
		line-height: 88rpx;
		background-color: #007AFF;
		color: #fff;
		font-size: 32rpx;
		border-radius: 44rpx;
		margin-top: 60rpx;
	}
	
	.register-btn:active {
		opacity: 0.8;
	}
	
	.login-link {
		text-align: center;
		margin-top: 30rpx;
		font-size: 28rpx;
		color: #666;
	}
	
	.link {
		color: #007AFF;
		margin-left: 10rpx;
	}
</style> 